<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>异地教师</title>
    <meta HTTP-EQUIV="pragma" CONTENT="no-cache">
    <meta HTTP-EQUIV="Cache-Control" CONTENT="no-store, must-revalidate">
    <meta HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">
    <meta HTTP-EQUIV="expires" CONTENT="0">

    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../static/css/font.css">
    <link rel="stylesheet" href="../static/css/weadmin.css">
    <script src="../lib/layui/layui.js" charset="utf-8"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <link rel="Shortcut Icon" href="images/favicon.ico"/>

    <link rel="stylesheet" href="../../src/css/layui.css">
    <link rel="stylesheet" href="../src/css/layui.css">
    <script src="../../src/layui.js"></script>
    <script src="../src/layui.js"></script>
    <script src="../../js/jquery-3.2.1.js"></script>
    <script src="../js/jquery-3.2.1.js"></script>
    <link rel="stylesheet" href="css/panel.css">

    <!--必须放最后-->
    <script src="../../js/setting.js"></script>
    <script src="../js/setting.js"></script>
</head>

<body>
<div class="weadmin-body">

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>落地教师</legend>
    </fieldset>
    <form class="layui-form " action="" method="post">
        <div class="layui-form-item ">
            <div class=" layui-input-inline">
                <select name="college" lay-search="">
                </select>
            </div>
            <div class=" layui-input-inline">
                <input type="text" name="teacher" placeholder="教师" autocomplete="off" class="layui-input">
            </div>
            <div class=" layui-input-inline">
                <button class="layui-btn" lay-submit lay-filter="search">查询</button>
            </div>
        </div>
    </form>


    <div class="layui-btn-group ">
        <!--//刷新-->
        <a class="layui-btn layui-btn-sm" href="javascript:location.replace(location.href);">
            <i class="layui-icon">&#x1002;</i>
        </a>
        <!-- //添加-->
        <button class="layui-btn layui-btn-sm" onclick="ToolBar.add(
            {
                'tid':'*教师号',
                'name':'*姓名',
                'college':'*院系',
                'note':'备注'
            },
            '/teacher/add/0'
            );">
            <i class="layui-icon">&#xe654;</i>
        </button>
        <!--//删除-->
        <button class="layui-btn layui-btn-sm"
                onclick="ToolBar.remove('teachTable','/teacher/delete/list');">
            <i class="layui-icon">&#xe640;</i>
        </button>
    </div>
    <table class="layui-table" lay-data="" id="teachTable" lay-filter="teachTable"></table>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-primary layui-btn-mini" lay-event="save">保存</a>
        <a class="layui-btn layui-btn-primary layui-btn-mini" lay-event="sum">查看汇总</a>
        <a class="layui-btn layui-btn-danger layui-btn-mini" lay-event="del">删除</a>
    </script>


</div>
<script>
    layui.use(['table', 'jquery', 'form'], function () {
        var form = layui.form;
        var table = layui.table;
        var index;
        var $ = layui.$;
        $.ajax({
            method: "get"
            , url: "/teacher/college"
            , success: function (res) {
                var s = "<option value=\"\" selected>院系</option>\n";
                for (var i = 0; i < res['count']; i++) {
                    s += "<option value=\"" + res['data'][i] + "\" >" + res['data'][i] + "</option>\n";
                }
                $("select[name = 'college']").html(s);
                form.render();
            }
            , error: function (res) {
                layer.msg("请刷新页面");
            }
        });


        $("#teachTable").ready(function () {
            index = layer.msg('加载中', {
                icon: 16
                , shade: 0.01
            });
        });
        var tableParam = { //其它参数在此省略
            id: 'teachTable',
            elem: '#teachTable',
            url: '/teacher/list/remote',
            page: true,
            limits: [10, 20, 50, 100, 300],
            limit: 10,
            response: {
                statusName: 'status' //数据状态的字段名称，默认：code
                , statusCode: 0 //成功的状态码，默认：0
                , msgName: 'msg' //状态信息的字段名称，默认：msg
                , countName: 'count' //数据总数的字段名称，默认：count
                , dataName: 'data' //数据列表的字段名称，默认：data
            }
            /*            ,height:'full-336',*/
            , cols: [[
                {checkbox: true, fixed: true},
                {field: 'id', fixed: true, width: '130', title: 'ID'},
                {field: 'tid', edit: true, width: '130', title: '教师号'},
                {field: 'name', edit: true, width: 130, title: '姓名'},
                {field: 'college', edit: true, width: 130, title: '院系'},
                {field: 'note', edit: true, width: '130', title: '备注'},
                {title: '操作', width: 200, align: 'center', toolbar: '#barDemo'}
            ]]
            , done: function (res, curr, count) {
                console.log(res);
                layer.close(layer.index);
            }
        };
        var tableObj = table.render(tableParam);
        form.render();

        table.on('edit(teachTable)', function (obj) {
            var value = obj.value //得到修改后的值
                , data = obj.data //得到所在行所有键值
                , field = obj.field; //得到字段
            //人数
            layer.msg('[教师: ' + data.teacher + '] ' + field + ' 更改为：' + value + "<br>确认修改后点击保存。");
        });
        table.on('tool(teachTable)', function (obj) {
            var data = obj.data;
            var tr = obj.tr;
            if (obj.event === 'save') {
                //layer.msg('保存：<br>'+ JSON.stringify(data));
                //ajax上传服务器
                //$('.layui-laypage-btn').click();

                $.ajax({
                    type: 'post'
                    , url: "/teacher"
                    , data: data
                    , dataType: 'json'
                    , contentType: 'application/x-www-form-urlencoded;charset=UTF-8'
                    , success: function (res) {
                        if (res["status"] == 1) {
                            layer.msg(res['msg'], {
                                icon: 2,
                                time: 1000
                            });
                        } else {
                            layer.msg(res['msg'], {
                                icon: 1,
                                time: 1000
                            });
                        }
                        setTimeout("$('.layui-laypage-btn').click();", 2000)
                    }
                    , error: function (res) {
                        layer.msg(res['msg']);
                        setTimeout("$('.layui-laypage-btn').click();", 2000)
                    }
                });
            } else if (obj.event === 'del') {
                layer.confirm('真的删除该行么', function (sure) {
                    $.ajax({
                        type: 'post'
                        , url: "/teacher/delete"
                        , data: data
                        , dataType: 'json'
                        , contentType: 'application/x-www-form-urlencoded;charset=UTF-8'
                        , success: function (res) {
                            if (res["status"] == 1) {
                                layer.msg(res['msg'], {
                                    icon: 2,
                                    time: 1000
                                });
                            } else {
                                layer.msg(res['msg'], {
                                    icon: 1,
                                    time: 1000
                                });
                            }
                        }
                        , error: function (res) {
                            layer.msg(res['msg']);
                        }
                    });
                    obj.del();
                    layer.close(sure);
                    $('.layui-laypage-btn').click();
                });
            } else if (obj.event === 'sum') {
                var title = "汇总";
                var url = "../total.html";
                var id = 19;

                localStorage.setItem("location", "false");
                localStorage.setItem("teacher", obj.data.name);

                parent.layui.admin.tab.tabDelete(id);
                parent.layui.admin.tab.tabAdd(title, url, id);
                parent.layui.admin.tab.tabChange(id);

                window.reload();


            }
            return false;
        });
        //监听提交
        form.on('submit(search)', function (data) {
            var college = data.field.college.trim(), teacher = data.field.teacher.trim();
            if (college.length < 1) {
                college = "null";
            }
            if (teacher.length < 1) {
                teacher = "null";
            }
            var urls = "/teacher/" + college + "/" + teacher + "/0";
            tableParam.url = urls;
            table.render(tableParam);
            return false;
        });
    });

</script>
<script type="text/html" id="practiceTmpl">
    {{d.place}}<i class="layui-icon table-select-icon"></i>
</script>

</body>

</html>